package com.gulimall.order.listener;

import com.gulimall.common.constrant.SeckillConstant;
import com.gulimall.common.to.rabbit.SekcillOrderTo;
import com.gulimall.order.service.OrderService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author kkk
 * @date 2022/8/11
 */
@Component
@RabbitListener(queues = SeckillConstant.SECKILL_ORDER_QUEUE)
public class SeckillOrderListener {
    @Autowired
    private OrderService orderService;

    @RabbitHandler
    public void seckillOrder(SekcillOrderTo sekcillOrderTo, Channel channel, Message message) throws IOException {
        try {
            orderService.saveSeckillOrder(sekcillOrderTo);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            e.printStackTrace();
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
        }
    }
}
